import VueRouter from "vue-router";
import Vue from "vue";
import Login from "../pages/Login.vue"
import Admin from "../pages/Admin.vue"
import Child1 from '../pages/Child1.vue'
import Child2 from '../pages/Child2.vue'
import Child3 from '../pages/Child3.vue'
import Child4 from '../pages/Child4.vue'
import Child5 from '../pages/Child5.vue'
import Child6 from '../pages/Child6.vue'



// 给vue 安装路由插件
Vue.use(VueRouter)
// 创建路由实例对象
const router = new VueRouter({
    mode:'hash',
    // 配置路由
    routes:[
        // redirect:重定向
        {path:'/',redirect:'/admin'},
        {
            path:'/login' , 
            name:'login' , 
            component:Login,
            meta:{
                title:'登录'
            }
        },
        {
            path:'/admin' , 
            name:'admin' , 
            component:Admin,
            redirect:'/admin/child1',
            children:[
                { path: '/admin/child1', name: 'child1', component: Child1 },
                { path: '/admin/child2', name: 'child2', component: Child2 },
                { path: '/admin/child3', name: 'child3', component: Child3 },
                { path: '/admin/child4', name: 'child4', component: Child4 },
                { path: '/admin/child5', name: 'child5', component: Child5 },
                { path: '/admin/child6', name: 'child6', component: Child6 }        
            ],
        }
    ]
})

//全局前置守卫
router.beforeEach((to,from,next)=>{
//    console.log(to,from)
    //拦截数量较少
   if(to.name ==='admin') {
    const token = localStorage.getItem('token')
    if(!token) {
        next('/login')
        alert('请登录')
        return
        }
   }

    if(to.meta.title) {
        document.title = to.meta.title
    }
//     // 拦截数量多
//     // console.log('to', to)
//     if(to.meta.isAuth) {
//         // 获取登录的本地存储
//         const token = localStorage.getItem('token')
//         if(!token) {
//             next({
//                 path:'/login',
//                 query:{
//                     redirectUrl:to.fullPath
//                 }
//             })
//             return
//             }
//         }
   next()
})
// window.router = router
export default router